home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / formatter.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  18.7 KB  |  603 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Generic output formatting.
  5.  
  6. Formatter objects transform an abstract flow of formatting events into
  7. specific output events on writer objects. Formatters manage several stack
  8. structures to allow various properties of a writer object to be changed and
  9. restored; writers need not be able to handle relative changes nor any sort
  10. of ``change back'' operation. Specific writer properties which may be
  11. controlled via formatter objects are horizontal alignment, font, and left
  12. margin indentations. A mechanism is provided which supports providing
  13. arbitrary, non-exclusive style settings to a writer as well. Additional
  14. interfaces facilitate formatting events which are not reversible, such as
  15. paragraph separation.
  16.  
  17. Writer objects encapsulate device interfaces. Abstract devices, such as
  18. file formats, are supported as well as physical devices. The provided
  19. implementations all work with abstract devices. The interface makes
  20. available mechanisms for setting the properties which formatter objects
  21. manage and inserting data into the output.
  22. """
  23. import sys
  24. AS_IS = None
  25.  
  26. class NullFormatter:
  27.     """A formatter which does nothing.
  28.  
  29.     If the writer parameter is omitted, a NullWriter instance is created.
  30.     No methods of the writer are called by NullFormatter instances.
  31.  
  32.     Implementations should inherit from this class if implementing a writer
  33.     interface but don't need to inherit any implementation.
  34.  
  35.     """
  36.     
  37.     def __init__(self, writer = None):
  38.         if writer is None:
  39.             writer = NullWriter()
  40.         
  41.         self.writer = writer
  42.  
  43.     
  44.     def end_paragraph(self, blankline):
  45.         pass
  46.  
  47.     
  48.     def add_line_break(self):
  49.         pass
  50.  
  51.     
  52.     def add_hor_rule(self, *args, **kw):
  53.         pass
  54.  
  55.     
  56.     def add_label_data(self, format, counter, blankline = None):
  57.         pass
  58.  
  59.     
  60.     def add_flowing_data(self, data):
  61.         pass
  62.  
  63.     
  64.     def add_literal_data(self, data):
  65.         pass
  66.  
  67.     
  68.     def flush_softspace(self):
  69.         pass
  70.  
  71.     
  72.     def push_alignment(self, align):
  73.         pass
  74.  
  75.     
  76.     def pop_alignment(self):
  77.         pass
  78.  
  79.     
  80.     def push_font(self, x):
  81.         pass
  82.  
  83.     
  84.     def pop_font(self):
  85.         pass
  86.  
  87.     
  88.     def push_margin(self, margin):
  89.         pass
  90.  
  91.     
  92.     def pop_margin(self):
  93.         pass
  94.  
  95.     
  96.     def set_spacing(self, spacing):
  97.         pass
  98.  
  99.     
  100.     def push_style(self, *styles):
  101.         pass
  102.  
  103.     
  104.     def pop_style(self, n = 1):
  105.         pass
  106.  
  107.     
  108.     def assert_line_data(self, flag = 1):
  109.         pass
  110.  
  111.  
  112.  
  113. class AbstractFormatter:
  114.     '''The standard formatter.
  115.  
  116.     This implementation has demonstrated wide applicability to many writers,
  117.     and may be used directly in most circumstances.  It has been used to
  118.     implement a full-featured World Wide Web browser.
  119.  
  120.     '''
  121.     
  122.     def __init__(self, writer):
  123.         self.writer = writer
  124.         self.align = None
  125.         self.align_stack = []
  126.         self.font_stack = []
  127.         self.margin_stack = []
  128.         self.spacing = None
  129.         self.style_stack = []
  130.         self.nospace = 1
  131.         self.softspace = 0
  132.         self.para_end = 1
  133.         self.parskip = 0
  134.         self.hard_break = 1
  135.         self.have_label = 0
  136.  
  137.     
  138.     def end_paragraph(self, blankline):
  139.         if not self.hard_break:
  140.             self.writer.send_line_break()
  141.             self.have_label = 0
  142.         
  143.         if self.parskip < blankline and not (self.have_label):
  144.             self.writer.send_paragraph(blankline - self.parskip)
  145.             self.parskip = blankline
  146.             self.have_label = 0
  147.         
  148.         self.hard_break = self.nospace = self.para_end = 1
  149.         self.softspace = 0
  150.  
  151.     
  152.     def add_line_break(self):
  153.         if not self.hard_break or self.para_end:
  154.             self.writer.send_line_break()
  155.             self.have_label = self.parskip = 0
  156.         
  157.         self.hard_break = self.nospace = 1
  158.         self.softspace = 0
  159.  
  160.     
  161.     def add_hor_rule(self, *args, **kw):
  162.         if not self.hard_break:
  163.             self.writer.send_line_break()
  164.         
  165.         self.writer.send_hor_rule(*args, **kw)
  166.         self.hard_break = self.nospace = 1
  167.         self.have_label = self.para_end = self.softspace = self.parskip = 0
  168.  
  169.     
  170.     def add_label_data(self, format, counter, blankline = None):
  171.         if self.have_label or not (self.hard_break):
  172.             self.writer.send_line_break()
  173.         
  174.         if not self.para_end:
  175.             if not blankline or 1:
  176.                 pass
  177.             self.writer.send_paragraph(0)
  178.         
  179.         if isinstance(format, str):
  180.             self.writer.send_label_data(self.format_counter(format, counter))
  181.         else:
  182.             self.writer.send_label_data(format)
  183.         self.nospace = self.have_label = self.hard_break = self.para_end = 1
  184.         self.softspace = self.parskip = 0
  185.  
  186.     
  187.     def format_counter(self, format, counter):
  188.         label = ''
  189.         for c in format:
  190.             if c == '1':
  191.                 label = label + '%d' % counter
  192.                 continue
  193.             if c in 'aA':
  194.                 if counter > 0:
  195.                     label = label + self.format_letter(c, counter)
  196.                 
  197.             counter > 0
  198.             if c in 'iI':
  199.                 if counter > 0:
  200.                     label = label + self.format_roman(c, counter)
  201.                 
  202.             counter > 0
  203.             label = label + c
  204.         
  205.         return label
  206.  
  207.     
  208.     def format_letter(self, case, counter):
  209.         label = ''
  210.         while counter > 0:
  211.             (counter, x) = divmod(counter - 1, 26)
  212.             s = chr(ord(case) + x)
  213.             label = s + label
  214.         return label
  215.  
  216.     
  217.     def format_roman(self, case, counter):
  218.         ones = [
  219.             'i',
  220.             'x',
  221.             'c',
  222.             'm']
  223.         fives = [
  224.             'v',
  225.             'l',
  226.             'd']
  227.         (label, index) = ('', 0)
  228.         while counter > 0:
  229.             (counter, x) = divmod(counter, 10)
  230.             if x == 9:
  231.                 label = ones[index] + ones[index + 1] + label
  232.             elif x == 4:
  233.                 label = ones[index] + fives[index] + label
  234.             elif x >= 5:
  235.                 s = fives[index]
  236.                 x = x - 5
  237.             else:
  238.                 s = ''
  239.             s = s + ones[index] * x
  240.             label = s + label
  241.             index = index + 1
  242.         if case == 'I':
  243.             return label.upper()
  244.         return label
  245.  
  246.     
  247.     def add_flowing_data(self, data):
  248.         if not data:
  249.             return None
  250.         prespace = data[:1].isspace()
  251.         postspace = data[-1:].isspace()
  252.         data = ' '.join(data.split())
  253.         if self.nospace and not data:
  254.             return None
  255.         self.softspace = postspace
  256.         self.writer.send_flowing_data(data)
  257.  
  258.     
  259.     def add_literal_data(self, data):
  260.         if not data:
  261.             return None
  262.         if self.softspace:
  263.             self.writer.send_flowing_data(' ')
  264.         
  265.         self.hard_break = data[-1:] == '\n'
  266.         self.nospace = self.para_end = self.softspace = self.parskip = self.have_label = 0
  267.         self.writer.send_literal_data(data)
  268.  
  269.     
  270.     def flush_softspace(self):
  271.         if self.softspace:
  272.             self.hard_break = self.para_end = self.parskip = self.have_label = self.softspace = 0
  273.             self.nospace = 1
  274.             self.writer.send_flowing_data(' ')
  275.         
  276.  
  277.     
  278.     def push_alignment(self, align):
  279.         if align and align != self.align:
  280.             self.writer.new_alignment(align)
  281.             self.align = align
  282.             self.align_stack.append(align)
  283.         else:
  284.             self.align_stack.append(self.align)
  285.  
  286.     
  287.     def pop_alignment(self):
  288.         if self.align_stack:
  289.             del self.align_stack[-1]
  290.         
  291.         if self.align_stack:
  292.             self.align = align = self.align_stack[-1]
  293.             self.writer.new_alignment(align)
  294.         else:
  295.             self.align = None
  296.             self.writer.new_alignment(None)
  297.  
  298.     
  299.     def push_font(self, .1):
  300.         (size, i, b, tt) = .1
  301.         if self.softspace:
  302.             self.hard_break = self.para_end = self.softspace = 0
  303.             self.nospace = 1
  304.             self.writer.send_flowing_data(' ')
  305.         
  306.         if self.font_stack:
  307.             (csize, ci, cb, ctt) = self.font_stack[-1]
  308.             if size is AS_IS:
  309.                 size = csize
  310.             
  311.             if i is AS_IS:
  312.                 i = ci
  313.             
  314.             if b is AS_IS:
  315.                 b = cb
  316.             
  317.             if tt is AS_IS:
  318.                 tt = ctt
  319.             
  320.         
  321.         font = (size, i, b, tt)
  322.         self.font_stack.append(font)
  323.         self.writer.new_font(font)
  324.  
  325.     
  326.     def pop_font(self):
  327.         if self.font_stack:
  328.             del self.font_stack[-1]
  329.         
  330.         if self.font_stack:
  331.             font = self.font_stack[-1]
  332.         else:
  333.             font = None
  334.         self.writer.new_font(font)
  335.  
  336.     
  337.     def push_margin(self, margin):
  338.         self.margin_stack.append(margin)
  339.         fstack = filter(None, self.margin_stack)
  340.         if not margin and fstack:
  341.             margin = fstack[-1]
  342.         
  343.         self.writer.new_margin(margin, len(fstack))
  344.  
  345.     
  346.     def pop_margin(self):
  347.         if self.margin_stack:
  348.             del self.margin_stack[-1]
  349.         
  350.         fstack = filter(None, self.margin_stack)
  351.         if fstack:
  352.             margin = fstack[-1]
  353.         else:
  354.             margin = None
  355.         self.writer.new_margin(margin, len(fstack))
  356.  
  357.     
  358.     def set_spacing(self, spacing):
  359.         self.spacing = spacing
  360.         self.writer.new_spacing(spacing)
  361.  
  362.     
  363.     def push_style(self, *styles):
  364.         if self.softspace:
  365.             self.hard_break = self.para_end = self.softspace = 0
  366.             self.nospace = 1
  367.             self.writer.send_flowing_data(' ')
  368.         
  369.         for style in styles:
  370.             self.style_stack.append(style)
  371.         
  372.         self.writer.new_styles(tuple(self.style_stack))
  373.  
  374.     
  375.     def pop_style(self, n = 1):
  376.         del self.style_stack[-n:]
  377.         self.writer.new_styles(tuple(self.style_stack))
  378.  
  379.     
  380.     def assert_line_data(self, flag = 1):
  381.         self.nospace = self.hard_break = not flag
  382.         self.para_end = self.parskip = self.have_label = 0
  383.  
  384.  
  385.  
  386. class NullWriter:
  387.     '''Minimal writer interface to use in testing & inheritance.
  388.  
  389.     A writer which only provides the interface definition; no actions are
  390.     taken on any methods.  This should be the base class for all writers
  391.     which do not need to inherit any implementation methods.
  392.  
  393.     '''
  394.     
  395.     def __init__(self):
  396.         pass
  397.  
  398.     
  399.     def flush(self):
  400.         pass
  401.  
  402.     
  403.     def new_alignment(self, align):
  404.         pass
  405.  
  406.     
  407.     def new_font(self, font):
  408.         pass
  409.  
  410.     
  411.     def new_margin(self, margin, level):
  412.         pass
  413.  
  414.     
  415.     def new_spacing(self, spacing):
  416.         pass
  417.  
  418.     
  419.     def new_styles(self, styles):
  420.         pass
  421.  
  422.     
  423.     def send_paragraph(self, blankline):
  424.         pass
  425.  
  426.     
  427.     def send_line_break(self):
  428.         pass
  429.  
  430.     
  431.     def send_hor_rule(self, *args, **kw):
  432.         pass
  433.  
  434.     
  435.     def send_label_data(self, data):
  436.         pass
  437.  
  438.     
  439.     def send_flowing_data(self, data):
  440.         pass
  441.  
  442.     
  443.     def send_literal_data(self, data):
  444.         pass
  445.  
  446.  
  447.  
  448. class AbstractWriter(NullWriter):
  449.     '''A writer which can be used in debugging formatters, but not much else.
  450.  
  451.     Each method simply announces itself by printing its name and
  452.     arguments on standard output.
  453.  
  454.     '''
  455.     
  456.     def new_alignment(self, align):
  457.         print 'new_alignment(%r)' % (align,)
  458.  
  459.     
  460.     def new_font(self, font):
  461.         print 'new_font(%r)' % (font,)
  462.  
  463.     
  464.     def new_margin(self, margin, level):
  465.         print 'new_margin(%r, %d)' % (margin, level)
  466.  
  467.     
  468.     def new_spacing(self, spacing):
  469.         print 'new_spacing(%r)' % (spacing,)
  470.  
  471.     
  472.     def new_styles(self, styles):
  473.         print 'new_styles(%r)' % (styles,)
  474.  
  475.     
  476.     def send_paragraph(self, blankline):
  477.         print 'send_paragraph(%r)' % (blankline,)
  478.  
  479.     
  480.     def send_line_break(self):
  481.         print 'send_line_break()'
  482.  
  483.     
  484.     def send_hor_rule(self, *args, **kw):
  485.         print 'send_hor_rule()'
  486.  
  487.     
  488.     def send_label_data(self, data):
  489.         print 'send_label_data(%r)' % (data,)
  490.  
  491.     
  492.     def send_flowing_data(self, data):
  493.         print 'send_flowing_data(%r)' % (data,)
  494.  
  495.     
  496.     def send_literal_data(self, data):
  497.         print 'send_literal_data(%r)' % (data,)
  498.  
  499.  
  500.  
  501. class DumbWriter(NullWriter):
  502.     '''Simple writer class which writes output on the file object passed in
  503.     as the file parameter or, if file is omitted, on standard output.  The
  504.     output is simply word-wrapped to the number of columns specified by
  505.     the maxcol parameter.  This class is suitable for reflowing a sequence
  506.     of paragraphs.
  507.  
  508.     '''
  509.     
  510.     def __init__(self, file = None, maxcol = 72):
  511.         if not file:
  512.             pass
  513.         self.file = sys.stdout
  514.         self.maxcol = maxcol
  515.         NullWriter.__init__(self)
  516.         self.reset()
  517.  
  518.     
  519.     def reset(self):
  520.         self.col = 0
  521.         self.atbreak = 0
  522.  
  523.     
  524.     def send_paragraph(self, blankline):
  525.         self.file.write('\n' * blankline)
  526.         self.col = 0
  527.         self.atbreak = 0
  528.  
  529.     
  530.     def send_line_break(self):
  531.         self.file.write('\n')
  532.         self.col = 0
  533.         self.atbreak = 0
  534.  
  535.     
  536.     def send_hor_rule(self, *args, **kw):
  537.         self.file.write('\n')
  538.         self.file.write('-' * self.maxcol)
  539.         self.file.write('\n')
  540.         self.col = 0
  541.         self.atbreak = 0
  542.  
  543.     
  544.     def send_literal_data(self, data):
  545.         self.file.write(data)
  546.         i = data.rfind('\n')
  547.         if i >= 0:
  548.             self.col = 0
  549.             data = data[i + 1:]
  550.         
  551.         data = data.expandtabs()
  552.         self.col = self.col + len(data)
  553.         self.atbreak = 0
  554.  
  555.     
  556.     def send_flowing_data(self, data):
  557.         if not data:
  558.             return None
  559.         if not self.atbreak:
  560.             pass
  561.         atbreak = data[0].isspace()
  562.         col = self.col
  563.         maxcol = self.maxcol
  564.         write = self.file.write
  565.         for word in data.split():
  566.             if atbreak:
  567.                 if col + len(word) >= maxcol:
  568.                     write('\n')
  569.                     col = 0
  570.                 else:
  571.                     write(' ')
  572.                     col = col + 1
  573.             
  574.             write(word)
  575.             col = col + len(word)
  576.             atbreak = 1
  577.         
  578.         self.col = col
  579.         self.atbreak = data[-1].isspace()
  580.  
  581.  
  582.  
  583. def test(file = None):
  584.     w = DumbWriter()
  585.     f = AbstractFormatter(w)
  586.     if file is not None:
  587.         fp = open(file)
  588.     elif sys.argv[1:]:
  589.         fp = open(sys.argv[1])
  590.     else:
  591.         fp = sys.stdin
  592.     for line in fp:
  593.         if line == '\n':
  594.             f.end_paragraph(1)
  595.             continue
  596.         f.add_flowing_data(line)
  597.     
  598.     f.end_paragraph(0)
  599.  
  600. if __name__ == '__main__':
  601.     test()
  602.  
  603.